package com.ali8ba.security;

import java.util.ArrayList;
import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.ali8ba.security.domain.User;
import com.ali8ba.security.service.ISecurityMrgService;

public class UserDetailService implements UserDetailsService {

	@Autowired
	private ISecurityMrgService securityMrgService;

	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException, DataAccessException {

		User user = securityMrgService.findUserById(username);
		System.out.println(user);

		Collection<GrantedAuthority> auths = new ArrayList<GrantedAuthority>();

		org.springframework.security.core.userdetails.User details = new org.springframework.security.core.userdetails.User(
				user.getUserId(), user.getPassword(), true, true, true,true,
				auths);

		return details;
	}

}
